$(function () {
    let ws;
    if("WebSocket" in window) {
        // 获取招聘者id
        let clientID = $("#eid").val();
        // 创建WebSocket对象,需要注意请求路径
        ws = new WebSocket("ws://127.0.0.1:8081/hire/web/" + clientID);
        // 接收到服务端消息时触发
        ws.onmessage = function (evt) {
            let received_msg = evt.data;
            layer.msg(received_msg,{
                offset: '50px'
            });
        };
    }
});
layui.use(["jquery", "table", "form", 'layer', 'laydate'], function () {
    let $ = layui.$;
    let table = layui.table;
    let form = layui.form;
    let layer = layui.layer;
    let laydate = layui.laydate;
    let id = $("#id").val();
    laydate.render({
        elem: '#cal',
        type: 'datetime'
    });
    let unprocessed = table.render({
        elem: '#unprocessed',
        title: '未处理简历',
        url: "/hire/employer/resume/data?status=未处理&id=" + id,
        response: {
            statusName: 'code',
            statusCode: 200,
            msgName: 'msg',
            count: 'count',
            dataName: 'data'
        },
        height: "508px",
        page: true,
        limit: 8,
        limits: [2, 5, 8, 10, 20],
        cols: [[
            {type: 'checkbox', fixed: 'left'},
            {title: '序号', templet: '#xuhao', width: 80, align: 'center'},
            {
                field: 'recruitment_id', title: '职位id', hide: true, templet: function (res) {
                    return res.recruitment.id;
                }
            },
            {
                field: 'delivery_id', title: '简历投递id', hide: true, templet: function (res) {
                    return res.delivery.id;
                }
            },
            {
                field: 'resume_id', title: '简历id', hide: true, templet: function (res) {
                    return res.resume.id;
                }
            },
            {
                field: 'name', title: '姓名', align: 'center', templet: function (res) {
                    return res.resume.name;
                }
            },
            {
                field: 'sex', title: '性别', align: 'center', templet: function (res) {
                    return res.resume.sex;
                }
            },
            {
                field: 'identity', title: '身份', align: 'center', width: 80, templet: function (res) {
                    return res.resume.resIdentity;
                }
            },
            {
                field: 'salary', title: '薪资要求', align: 'center', templet: function (res) {
                    return res.resume.salaryRequirement;
                }
            },
            {
                field: 'state', title: '状态', align: 'center', templet: function (res) {
                    return res.resume.resState;
                }
            },
            {
                field: 'delivery_time', title: '投递时间', width: 180, align: 'center', templet: function (res) {
                    return res.delivery.deliveryTime;
                }
            },
            {field: 'relationName', toolbar: '#barDemo1', title: '操作', width: 380}
        ]],
        text: {none: '您暂时没有未处理的投递简历'}
    });
    table.render({
        elem: '#intentional',
        title: '有意向简历',
        url: "/hire/employer/resume/data?status=有意向&id=" + id,
        response: {
            statusName: 'code',
            statusCode: 200,
            msgName: 'msg',
            count: 'count',
            dataName: 'data'
        },
        height: "500px",
        page: true,
        limit: 10,
        limits: [2, 5, 8, 10, 20],
        cols: [[
            {type: 'checkbox', fixed: 'left'},
            {title: '序号', templet: '#xuhao', width: 80, align: 'center'},
            {
                field: 'recruitment_id', title: '职位id', hide: true, templet: function (res) {
                    return res.recruitment.id;
                }
            },
            {
                field: 'delivery_id', title: '简历投递id', hide: true, templet: function (res) {
                    return res.delivery.id;
                }
            },
            {
                field: 'resume_id', title: '简历id', hide: true, templet: function (res) {
                    return res.resume.id;
                }
            },
            {
                field: 'name', title: '姓名', align: 'center', templet: function (res) {
                    return res.resume.name;
                }
            },
            {
                field: 'sex', title: '性别', align: 'center', templet: function (res) {
                    return res.resume.sex;
                }
            },
            {
                field: 'identity', title: '身份', align: 'center', width: 80, templet: function (res) {
                    return res.resume.resIdentity;
                }
            },
            {
                field: 'salary', title: '薪资要求', align: 'center', templet: function (res) {
                    return res.resume.salaryRequirement;
                }
            },
            {
                field: 'state', title: '状态', align: 'center', templet: function (res) {
                    return res.resume.resState;
                }
            },
            {
                field: 'delivery_time', title: '投递时间', width: 180, align: 'center', templet: function (res) {
                    return res.delivery.deliveryTime;
                }
            },
            {field: 'relationName', toolbar: '#barDemo2', title: '操作', width: 300}
        ]],
        text: {none: '您暂时没有有意向的投递简历'}
    });
    table.render({
        elem: '#unintentional',
        title: '无意向简历',
        url: "/hire/employer/resume/data?status=无意向&id=" + id,
        response: {
            statusName: 'code',
            statusCode: 200,
            msgName: 'msg',
            count: 'count',
            dataName: 'data'
        },
        height: "500px",
        page: true,
        toolbar: '#toolbarDemo',
        limit: 10,
        limits: [2, 5, 8, 10, 20],
        cols: [[
            {type: 'checkbox', fixed: 'left'},
            {title: '序号', templet: '#xuhao', width: 80, align: 'center'},
            {
                field: 'recruitmentId', title: '职位id', hide: true, templet: function (res) {
                    return res.recruitment.id;
                }
            },
            {
                field: 'resume_id', title: '简历id', hide: true, templet: function (res) {
                    return res.resume.id;
                }
            },
            {
                field: 'delivery_id', title: '简历投递id', hide: true, templet: function (res) {
                    return res.delivery.id;
                }
            },
            {
                field: 'name', title: '姓名', align: 'center', templet: function (res) {
                    return res.resume.name;
                }
            },
            {
                field: 'sex', title: '性别', align: 'center', templet: function (res) {
                    return res.resume.sex;
                }
            },
            {
                field: 'identity', title: '身份', align: 'center', width: 80, templet: function (res) {
                    return res.resume.resIdentity;
                }
            },
            {
                field: 'salary', title: '薪资要求', align: 'center', templet: function (res) {
                    return res.resume.salaryRequirement;
                }
            },
            {
                field: 'state', title: '状态', align: 'center', templet: function (res) {
                    return res.resume.resState;
                }
            },
            {
                field: 'deliveryTime', title: '投递时间', width: 180, align: 'center', templet: function (res) {
                    return res.delivery.deliveryTime;
                }
            },
            {field: 'relationName', toolbar: '#barDemo3', title: '操作', width: 300}
        ]],
        text: {none: '您暂时没有无意向的投递简历'},
    });
    form.on('select(selectTime1)', function(data){
        let v = data.value;
        table.reload('unprocessed',{
            where:{getDay:v}
        });
    });
    form.on('select(selectTime2)', function(data){
        let v = data.value;
        table.reload('intentional',{
            where:{getDay:v}
        });
    });
    form.on('select(selectTime3)', function(data){
        let v = data.value;
        table.reload('unintentional',{
            where:{getDay:v}
        });
    });
    table.on('tool(unprocessed)', function (obj) {
        let resumeId = obj.data.resume.id;
        let deliveryId = obj.data.delivery.id;
        if (obj.event == 'view') {
            $.post('/hire/employer/resume/view',{resumeId:resumeId},function (res) {
                if(res.code=='200'){
                    window.open('/hire/resume/resume_preview?resumeId='+resumeId);
                }else{
                    layer.msg("很抱歉,请重试");
                }
            });
        } else if (obj.event == 'yes') {
            $.post('/hire/employer/resume/update/status', {id: deliveryId, status: '有意向'}, function (res) {
                if (res.code == '200') {
                    layer.msg("已设为有意向", {icon: 6});
                    obj.del();
                    // table.reload('unprocessed', {
                    //     url: '/hire/employer/resume/data?status=未处理&id=' + id,
                    // });
                    table.reload('unintentional', {
                        url: '/hire/employer/resume/data?status=无意向&id=' + id,
                    });
                    table.reload('intentional', {
                        url: '/hire/employer/resume/data?status=有意向&id=' + id,
                    });
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            });
        } else if (obj.event == 'no') {
            $.post('/hire/employer/resume/update/status', {id: deliveryId, status: '无意向'}, function (res) {
                if (res.code == '200') {
                    layer.msg("已设为无意向", {icon: 5});
                    obj.del();
                    table.reload('unprocessed', {
                        ulr: '/hire/employer/resume/data?status=未处理&id=' + id,
                    });
                    table.reload('unintentional', {
                        ulr: '/hire/employer/resume/data?status=无意向&id=' + id,
                    });
                    table.reload('intentional', {
                        ulr: '/hire/employer/resume/data?status=有意向&id=' + id,
                    });
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            })
        } else if (obj.event == 'black') {
            layer.confirm('请确认是否取屏蔽该用户', {
                btn: ['确认', '取消'] //按钮
            }, function () {
                $.post('/hire/employer/add/blacklist', {resumeId: resumeId}, function (res) {
                    if (res.code == '200') {
                        layer.msg("屏蔽成功", {icon: 1})
                    } else if (res.code == '500') {
                        layer.msg(res.msg, {icon: 2})
                    } else {
                        layer.msg(res.msg, {icon: 0})
                    }
                })
            });
        }

    });
    table.on('tool(intentional)', function (obj) {
        if (obj.event == 'view') {
            let resumeId = obj.data.resume.id;
            $.post('/hire/employer/resume/view',{resumeId:resumeId},function (res) {
                if(res.code=='200'){
                    window.open('/hire/resume/resume_preview?resumeId='+resumeId);
                }else{
                    layer.msg("很抱歉,请重试");
                }
            });
        } else if (obj.event == 'interview') {
            form.val('interview-form', {
                "resumeId": obj.data.resume.id,
                "recruitmentId": obj.data.recruitment.id,
                "recruitment": obj.data.recruitment.name,
            });
            let index = layer.open({
                type: 1,
                title: '邀约面试',
                skin: 'layui-layer-molv',
                content: $('#interview-layer'),
                cancel: function () {
                    // 右上角关闭事件的逻辑
                    layer.close(index);
                    $("#interview-layer").css("display", "none");
                }
            });
            form.on('submit(interview-submit)', function () {
                let id = obj.data.delivery.id;
                let param = $("#interview-form").serialize();
                param = param + "&deliveryId=" + id;
                $.post('/hire/employer/resume/interview', param, function (res) {
                    console.log(param);
                    layer.close(index);
                    $("#interview-layer").css("display", "none");
                    if (res.code == '200') {
                        layer.msg('邀约成功', {icon: 6});
                        // table.reload('resume1');
                        obj.del();
                    } else {
                        layer.msg("很抱歉,请您重试", {icon: 5});
                    }
                });
                return false;
            })
        } else if (obj.event == 'no') {
            layer.confirm('请确认是否修改为无意向', {
                btn: ['确认', '取消'] //按钮
            }, function () {
                let deliveryId = obj.data.delivery.id;
                $.post('/hire/employer/resume/update/status', {id: deliveryId, status: '无意向'}, function (res) {
                    if (res.code == '200') {
                        layer.msg("已设为无意向", {icon: 5});
                        obj.del();
                        table.reload('intentional', {
                            ulr: '/hire/employer/resume/data?status=有意向&id=' + id,
                        });
                        table.reload('unintentional', {
                            ulr: '/hire/employer/resume/data?status=无意向&id=' + id,
                        });
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                });

            });
        }
    });
    table.on('toolbar(unintentional)', function (obj) {
        let checkStatus = table.checkStatus('unintentional');
        let data = checkStatus.data;
        if (obj.event == 'delete') {
            if (data.length == 0) {
                layer.msg('请选择至少一份简历进行删除');
            } else {
                layer.confirm('请确认是否取删除投递记录', {
                    btn: ['确认', '取消'] //按钮
                }, function () {
                    let ids = '';
                    for (let i = 0; i < data.length; i++) {
                        if (i == data.length - 1) {
                            ids = ids + data[i].delivery.id;
                        } else {
                            ids = ids + data[i].delivery.id + ",";
                        }
                    }
                    $.post('/hire/employer/del/delivery/batch', {ids: ids}, function (res) {
                        if (res.code == '200') {
                            layer.msg("成功删除" + res.data + "条记录", {icon: 1});
                            table.reload('unintentional', {
                                ulr: '/hire/employer/resume/data?status=无意向&id=' + id,
                            });
                        } else {
                            layer.msg(res.msg);
                        }
                    });
                });
            }
        }
    });
    table.on('tool(unintentional)', function (obj) {
        // console.log(obj.event);
        if (obj.event == 'view') {
            let resumeId = obj.data.resume.id;
            $.post('/hire/employer/resume/view',{resumeId:resumeId},function (res) {
                if(res.code=='200'){
                    window.open('/hire/resume/resume_preview?resumeId='+resumeId);
                }else{
                    layer.msg("很抱歉,请重试");
                }
            });
        } else if (obj.event == 'delete') {
            layer.confirm('请确认是否取删除投递记录', {
                btn: ['确认', '取消'] //按钮
            }, function () {
                let deliveryId = obj.data.delivery.id;
                $.post('/hire/employer/resume/delivery/delete', {id: deliveryId}, function (res) {
                    if (res.code == '200') {
                        layer.msg("删除成功", {icon: 1});
                        obj.del();
                        // table.reload('unintentional', {
                        //     ulr: '/hire/employer/resume/data?status=无意向&id=' + id,
                        // });
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                });
            });
        } else if (obj.event == 'yes') {
            layer.confirm('请确认是否修改为有意向', {
                btn: ['确认', '取消'] //按钮
            }, function () {
                let deliveryId = obj.data.delivery.id;
                $.post('/hire/employer/resume/update/status', {id: deliveryId, status: '有意向'}, function (res) {
                    if (res.code == '200') {
                        layer.msg("已设为有意向", {icon: 6});
                        obj.del();
                        // table.reload('unintentional', {
                        //     ulr: '/hire/employer/resume/data?status=无意向&id=' + id,
                        // });
                        table.reload('intentional', {
                            ulr: '/hire/employer/resume/data?status=有意向&id=' + id,
                        });

                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                });

            });
        }
    });
});
